<?php
/**
 * Celebrio 
 * 
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    Celebrio
 */
namespace Celebrio;

use Nette\Environment;
use Celebrio\ShadowReader;

/**
 * Class to hash passwords
 *
 * @author petr, pavel
 */
final class PasswordHasher {
    
    /**
     * Generates and returns the database password for the user identified by $username.
     *
     * @param string $username
     * @param string $hashedPassword the user password
     * @return string the database machine password
     */
    public static function databaseHash($username, $hashedPassword) {
        $shadowReader = new ShadowReader();
        $dbSalt = $shadowReader->getPassword("dbSalt");
        $password = hash('sha1',$dbSalt.$hashedPassword.(self::generatePass($username)));
        return "*".$password;
    }

    public static function userHash($username, $password) {
        $shadowReader = new ShadowReader();
        $userSalt = $shadowReader->getPassword("userSalt");
        $password = hash('sha512', self::generatePass($username).$password.$userSalt);
        return $password;
    }

    private static function generatePass($username) {
        return hash("sha1",$username);
    }
}
